<template>
  <uni-nav-bar title="售液记录" left-icon="left" @clickLeft="navBack" :fixed="true" statusBar></uni-nav-bar>
  <view class="package_sale direction-start">
    <view class="datas space-between">
      <uni-datetime-picker v-model="range" type="daterange" :border="false" :clear-icon="false" rangeSeparator="至"
        @change="change" />
      <uni-icons type="bars" size="30" color="#666" @click="screenPopupOpen()"></uni-icons>
    </view>
    <view class="center del" v-if="name">
      <view @click="del()">
        <text>{{name}}</text>
        <uni-icons type="closeempty" size="18" color="#999"></uni-icons>
      </view>
    </view>
  </view>
  <scroll-view scroll-y="true" enable-back-to-top @scrolltolower="loadMore" class="scroll-box"
    :style="{ height: pageHeight + 'px' }">
    <SellList :getUreaLog="orderLists"></SellList>
    <Popu v-if="orderLists.length<1" :pageHeight="pageHeight" text="暂无订单"></Popu>
  </scroll-view>
  <!-- 筛选弹框 -->
  <uni-popup ref="popup" type="bottom" background-color="#fff">
    <view style="padding:20rpx;box-sizing: border-box;">
      <view style="height:400rpx;overflow: auto;">
        <view style="margin-top:20rpx">
          <view>加注机</view>
          <view class="center list"><text v-for="item in fillingInfo.getFillList" :key="item.id"
              @click="choose('1',item)"
              :style="{background:active.name==item.name?'#1B91FF':'#eee',color:active.name==item.name?'#fff':'#333'}">{{item.name}}</text>
          </view>
        </view>
      </view>
      <view class="space-between">
        <text class="sure" @click="choose('4')">重置</text>
        <text class="sure" style="background-color:#1B91FF;color:#fff;" @click="choose('5')">确认</text>
      </view>
    </view>
  </uni-popup>
</template>

<script setup lang="ts">
  import { navBack } from '@/utils/navigator';
  import { ref, onMounted } from 'vue';
  import SellList from '@c/rehydrator/sellList.vue';
  import { format, subDays } from 'date-fns';
   import Popu from '@c/earthPushing/common/popu.vue';
  import { getFillingList } from '@/gql/rehydrator';
  import { toPublish } from '@mqtt';
  import { fillingStation } from '@/stores/rehydrator';
  const fillingInfo = fillingStation();
  /**
   * 加注机详情页面
   * @param {Number} pageHeight 列表高度
   * @param {Number} lastPage 总页数
   * @param {Number} currentPage 当前页数
   * @param {Array} orderLists 加注订单列表
   * @param {Null} popup 筛选弹框
   * @param {Object} active 筛选加注机
   * @param {Object} name 筛选加注机名称
   */
  // data
  const now = new Date();
  let end = format(now, 'yyyy-MM-dd');
  const daysAgo = subDays(now, 6);
  let start = format(daysAgo, 'yyyy-MM-dd');
  const range = ref([start, end]);

  const active = ref({ name: '', id: 0 })
  const name = ref('')
  const popup = ref(null)
  const pageHeight = ref()
  const isLoading = ref(false);
  const lastPage = ref(1);
  const currentPage = ref(1);
  const orderLists = ref([]);
  onMounted(() => {
    uni.getSystemInfo({
      success: function (res) {
        pageHeight.value = res.windowHeight - 130
      },
    });
    fillingInfo.Receiver()
    init()
  });
  /**
   * 获取售液记录列表
   */
  function init() {
    isLoading.value = true;
    const payload = {
      query: getFillingList,
      variables: {
        page: currentPage.value,
        start,
        end,
        delivery_id: fillingInfo.id,
        fill_id: active.value.id,
      },
    };
    toPublish(
      'ql/delivery/getFillingList',
      payload,
      (obj : any) => {
        const { getFillingList } = obj.data;
        isLoading.value = false;
        orderLists.value = [...orderLists.value, ...getFillingList.list];
        lastPage.value = getFillingList.lastpage;
      }
    );
  }
  /**
   * 加载更多
   */
  function loadMore() {
    if (currentPage.value < lastPage.value) {
      currentPage.value++;
      init();
    }
  }
  /**
   * 切换日期
   * @param {String} sta 开始时间
   * @param {String} en 结束时间
   */
  function change([sta, en] : any) {
    currentPage.value = 1;
    orderLists.value = [];
    start = sta;
    end = en;
    init();
  }
  /**
   * 选择筛选的信息
   * @param {String} type 筛选的信息
   * @param {Object} num 选中的信息
   */
  function choose(type : string, num : any) {
    switch (type) {
      case '1':
        active.value.name = num.name
        active.value.id = num.id
        break;
      case '4':
        active.value.name = ''
        active.value.id = 0
        break;
      case '5':
        currentPage.value = 1;
        orderLists.value = [];
        name.value = active.value.name
        init();
        screenPopupClose()
        break;
    }
  }
  /**
   * 删除筛选的信息
   */
  function del() {
    active.value.name = ''
    active.value.id = 0
    name.value = ''
    currentPage.value = 1;
    orderLists.value = [];
    init();
  }
  // 打开关闭筛选弹框
  function screenPopupOpen() {
    popup.value.open()
  }
  function screenPopupClose() {
    popup.value.close()
  }
</script>

<style scoped lang="less">
  .sure {
    margin-top: 40rpx;
    width: 341rpx;
    height: 78rpx;
    border: 2rpx solid #1B91FF;
    border-radius: 5px;
    box-sizing: border-box;
    text-align: center;
    line-height: 78rpx;
    color: #1B91FF;
  }

  .list {
    flex-wrap: wrap;

    text {
      width: 165rpx;
      text-align: center;
      height: 50rpx;
      line-height: 50rpx;
      background-color: #eee;
      border-radius: 5px;
      margin-top: 15rpx;
      margin-right: 15rpx;
      font-size: 24rpx;
    }

    text:nth-child(4n) {
      margin-right: 0px;
    }
  }

  .del {
    view {
      padding: 2rpx 20rpx;
      box-sizing: border-box;
      border-radius: 30rpx;
      background-color: #eee;
      margin-right: 20rpx;
    }
  }

  .fillings {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;

    image {
      width: 352rpx;
      height: 352rpx;
      margin-top: -140rpx;
    }

    text {
      font-size: 24rpx;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #999999;
      margin-top: 20rpx;
    }
  }
</style>
<style>
  .datas>>>.uni-date__x-input {
    font-size: 30rpx !important;
  }

  .datas>>>.uni-date-x {
    font-size: 30rpx !important;
  }

  .datas>>>.uni-icons {
    font-size: 48rpx !important;
  }
</style>